package cn.tedu.bonfire.front.payment.service.impl;

import cn.tedu.bonfire.common.enumerator.ServiceCode;
import cn.tedu.bonfire.common.ex.ServiceException;
import cn.tedu.bonfire.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.bonfire.front.payment.dao.persist.mapper.UserMapper;
import cn.tedu.bonfire.front.payment.dao.persist.repository.IUserRepository;
import cn.tedu.bonfire.front.payment.pojo.entity.User;
import cn.tedu.bonfire.front.payment.pojo.param.EditorPayParam;
import cn.tedu.bonfire.front.payment.pojo.param.RewardParam;
import cn.tedu.bonfire.front.payment.pojo.param.WithdrawalParam;
import cn.tedu.bonfire.front.payment.service.IUserService;
import com.alibaba.druid.sql.visitor.functions.If;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper mapper;
    @Autowired
    private IUserRepository userRepository;

    @Override
    public void reward(RewardParam rewardParam, CurrentPrincipal currentPrincipal) {
        Long articleIdId = rewardParam.getArticleId();
        Long authorId = userRepository.selectAuthorId(articleIdId);
        if (authorId.equals(currentPrincipal.getId())){
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,"不能给自己的作品打赏");
        }

        BigDecimal userBalance = mapper.selectBalance(currentPrincipal.getId());

        if (userBalance.compareTo(rewardParam.getRewardCount())!=1) {
            throw new ServiceException(ServiceCode.ERROR_CAPTCHA,"钱包余额不足");
        }
        User user = new User();
        user.setId(currentPrincipal.getId());
        user.setBalance(userBalance.subtract(rewardParam.getRewardCount()));
        mapper.updateById(user);

        BigDecimal authorBalance = mapper.selectBalance(authorId);

        User author = new User();
        author.setId(authorId);
        author.setBalance(authorBalance.add(rewardParam.getRewardCount()));
        mapper.updateById(author);
    }


    @Override
    public void payment(String totalPrice,Long userId) {
        BigDecimal balance = mapper.selectBalance(userId);
        BigDecimal price = new BigDecimal(totalPrice);
        User user=new User();
        user.setId(userId);
        user.setBalance(balance.add(price));
        int row = mapper.updateById(user);
        if (row!=1){
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,"充值失败");
        }
    }

    @Override
    public void editorPay(EditorPayParam editorPayParam, CurrentPrincipal currentPrincipal) {
        Long userId = currentPrincipal.getId();
        Integer hours = editorPayParam.getHours();
        Integer expiration = userRepository.selectExpiration(userId);
        BigDecimal userBalance = mapper.selectBalance(currentPrincipal.getId());
        if (userBalance.compareTo(editorPayParam.getPrice()) != 1) {
            throw new ServiceException(ServiceCode.ERROR_CAPTCHA, "钱包余额不足");
        }
        User user = new User();
        user.setId(currentPrincipal.getId());
        user.setBalance(userBalance.subtract(editorPayParam.getPrice()));
        user.setExpiration(expiration + hours);
        mapper.updateById(user);
    }

    @Override
    public boolean checkExpiration(CurrentPrincipal currentPrincipal) {
        Long userId = currentPrincipal.getId();
        Integer expiration = userRepository.selectExpiration(userId);
        return expiration != 0;
    }

    @Override
    public void updateExpiration() {
        List<Long> idList = userRepository.selectAllExpirationGreaterThanZero();
        for(Long id : idList){
            Integer oldExpiration = userRepository.selectExpiration(id);
            Integer newExpiration = oldExpiration - 12;
            if (newExpiration<0){
                newExpiration=0;
            }
            User user = new User();
            user.setId(id);
            user.setExpiration(newExpiration);
            mapper.updateById(user);
        }
    }

    @Override
    public int selectExpiration(CurrentPrincipal currentPrincipal) {
        Long userId = currentPrincipal.getId();
        return userRepository.selectExpiration(userId);
    }

    @Override
    public void withdrawal(CurrentPrincipal currentPrincipal) {
        BigDecimal balance = mapper.selectBalance(currentPrincipal.getId());
        if (balance.compareTo(new BigDecimal(100))==-1){
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,"提现金额不足100，提现失败");
        }
        BigDecimal remainderBalance = balance.remainder(new BigDecimal(100));
        User user = new User();
        user.setId(currentPrincipal.getId());
        user.setBalance(remainderBalance);
        mapper.updateById(user);
    }
}
